﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERTesting.Model.Config;

namespace ERTesting.Model.Result
{
    public class ScenarioResult : IEquatable<ScenarioResult>
    {

        #region Private Members

        private bool _initialised = false;
        private bool _isPass;

        #endregion Private Members

        #region Public Properties

        public string Id { get; private set; }

        public DateTime TimeStamp { get; private set; }

        public double RunTime { get; private set; }

        public bool IsError { get; private set; }
        public string BaselineId { get; set; }
        public Scenario Scenario { get; set; }

        public bool IsPass
        {
            get {
                return Initialised ? Outputs.All(testOutput => testOutput.IsSignedOff) : _isPass;
            }
        }

        private List<TestOutput> _outputs;
        public List<TestOutput> Outputs
        {
            get
            {
                if (!_initialised)
                {
                    throw new ApplicationException("Please initialise the result set with the outputs.");
                }
                return _outputs;
             }
            private set { _outputs = value; }
        }

        public bool Initialised
        {
            get { return _initialised; }
        }

        #endregion Public Properties

        #region Constructors

        public ScenarioResult(string id, DateTime timeStamp, double runTime, bool isError, bool isPass, string baselineId, Scenario scenario)
        {
            Id = id;
            TimeStamp = timeStamp;
            RunTime = runTime;
            IsError = isError;
            BaselineId = baselineId;
            Scenario = scenario;
            _isPass = isPass;
        }

        #endregion Constructors

        #region Public Methods

        public void Initialise(IEnumerable<TestOutput> outputs)
        {
            if(Initialised)
            {
                throw new ApplicationException("Do not call Initialise more than once!");
            }
            _initialised = true;
            Outputs = new List<TestOutput>(outputs);
        }

        public bool Equals(ScenarioResult other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.Id, Id);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (ScenarioResult)) return false;
            return Equals((ScenarioResult) obj);
        }

        public override int GetHashCode()
        {
            return (Id != null ? Id.GetHashCode() : 0);
        }

        public static bool operator ==(ScenarioResult left, ScenarioResult right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(ScenarioResult left, ScenarioResult right)
        {
            return !Equals(left, right);
        }

        #endregion Public Methods
    }
}